package top.hkyzf.onekeycommand.mixin;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import top.hkyzf.onekeycommand.util.Utils;

import java.awt.event.KeyEvent;

/**
 * description: 存在 GUI 界面时进行注入
 * @author zhufeng
 * @date 2022/9/7 14:57
 * @version V1.0
 */
@Mixin(Screen.class)
@Environment(EnvType.CLIENT)
public class ScreenMixin {
    private static final MinecraftClient CLIENT = MinecraftClient.getInstance();

    @Inject(method = "keyPressed", at = @At("HEAD"))
    private void onKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
        // 当键盘被按下则执行
        Utils.LOGGER.info("Screen : {} 键被按下了！", KeyEvent.getKeyText(keyCode));
        // 按下 F12 回默认家
        if (GLFW.GLFW_KEY_F12 == keyCode) {
            CLIENT.player.sendCommand("home Home");
        // 按下传送键回名字叫 safe 的家
        } else if (Utils.TRANSPORT_KEY.matchesKey(keyCode, scanCode)) {
            CLIENT.player.sendCommand("home safe");
        // 按下挂机键开始挂机
        } else if (Utils.ON_HOOK_KEY.matchesKey(keyCode, scanCode)) {
            Utils.LOGGER.info("开始挂机！");
            // 挂机过程中再次按挂机键则停止挂机
            /*if (CONTEXT.isInControl()) {
                ON_HOOK.forceStopHook();
                Utils.LOGGER.info("挂机完毕！");
            } else {
                ON_HOOK.onHookInNewThread();
                Utils.LOGGER.info("另起线程开始挂机！");
            }*/
        }
    }

}
